<?php 
namespace Member\Model;
use Think\Model;

class ReplyModel extends Model {

	protected $_validate = array(
			array('keyword', 'require', '关键词必须要填写！', self::MUST_VALIDATE),
			array('content', 'require', '回复内容必须要填写！', self::MUST_VALIDATE),
	);
	
	/* 自动完成规则 */
	protected $_auto = array(
			array('uid', 'is_login', self::MODEL_INSERT, 'function'),
			array('title', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
			array('create_time', NOW_TIME, self::MODEL_INSERT),
			array('update_time', NOW_TIME, self::MODEL_UPDATE),
	);
	
	function detail($id) {
		$info = $this->find($id);
		$type = $info['type'];
		$logicInfo = M('Reply_'.$type)->find($id);
		return array_merge($info, $logicInfo);
	}
	
	function update() {
		/* 获取数据对象 */
		$data = $this->create();
		if (empty($data)) {
			return false;
		}
		/* 添加或更新基础内容 */
		if(empty($data['id'])){
			$id = $this->add();
			if(!$id){
				$this->error = '新增基础内容出错！';
				return false;
			}
		}else {
			$status = $this->save();
			if(false === $status){
				$this->error = '更新基础内容出错！';
				return false;
			}
		}
		$logicModel = M('Reply_'.$data['type']);
		$logicModel->create();
		$logicModel->update_time = NOW_TIME;
		/* 添加或更新扩展内容 */
		if (empty($data['id'])) {
			$logicModel->id = $id;
			if (!$logicModel->add()) {
				$this->error = '新增扩展内容出错';
				return false;
			}
		}else {
			$logicModel->id = $data['id'];
			if (!$logicModel->save()) {
				$this->error = '更新扩展内容出错';
				return false;
			}
		}
		return $data;
	}
}